import scrapy

from ScrapyObject.spiders.utils.url_utils import *

'''
scrapy crawl ltag -o ltag.json
https://xbbc668e.73u5seku.top/
'''

class LtagSpider(scrapy.Spider):
    name = "ltag"
    # 前缀
    prefix = 'https://xbbc668e.'
    # 中缀
    website = '73u5seku'
    # 后缀
    suffix = '.top/'
    allowed_domains = [website + '.top']
    start_urls = [prefix + website + suffix]

    def __init__(self):
        self.i = 0

    def parse(self, response):
        # 获取字符串类型的网页内容
        content = get_data(response)
        video_list = get_video_url_one(content)
        if len(video_list):
            self.i = self.i + 1
            yield get_video_item(id=self.i, url=response.url, name='', pUrl='', vUrl=format_url_one(video_list[0]))
        url_list = response.xpath("//a[@title='在线播放']/@href").extract()
        picture_list = response.xpath("//div[@class='col-md-9']/img/@src").extract()
        name_list = response.xpath("//div[@class='col-md-9']/img/@title").extract()
        tag_list = response.xpath("//div[@class='row']/p/text()").extract()
        if len(url_list) and len(picture_list) and len(name_list) and len(tag_list):
            self.i = self.i + 1
            sdfsd = tag_list[0]
            if '類型：' in tag_list[0]:
                sdfsd = tag_list[0].replace("類型：", "")
            yield get_video_item(id=self.i, tags=sdfsd.strip(), url=split_joint(self.prefix + self.website + self.suffix, url_list[0]), name=name_list[0], pUrl=picture_list[0])
        # 从网页中提取url链接
        url_list = get_url(content)
        # 提取url
        for url in url_list:
            if url.startswith('/') or url.startswith('html'):
                yield scrapy.Request(split_joint(self.prefix + self.website + self.suffix, url), callback=self.parse)
            elif url.startswith('http') or url.startswith('www'):
                yield scrapy.Request(url, callback=self.parse)